package com.cotte.flow.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cotte.flow.model.DgraphRelations;
import com.cotte.flow.model.DgraphSteps;
import com.cotte.flow.model.Relation;
import com.cotte.flow.service.StepDgraphService;

import net.sf.json.JSONArray;

@RestController
@RequestMapping("/dgraph/step")
public class StepDgraphController {
	
	@Autowired
	private StepDgraphService service;
	
	@Value("${dgraph.steps.name.prefix}")
	private String STEP_NAME;
	@Value("${dgraph.relations.name.prefix}")
	private String RELATION_NAME;
	
	@GetMapping("/test")
	public void test() {
		service.getName();
	}
	/**
	 * version :v1 和 v2 代表两个版本的态势图
	 */
	
	/**
	 * 创建工序节点
	 */
	@GetMapping("/create")
	public String create(String department,String cate,String version) {
		String name = null;
		try {
			//name = "v1_F001_XF_20181129161331";
			name = service.setStep(department,cate,version);
			service.setRelation(department,cate,name);
			//service.addRelation(version);
		} catch (Exception e) {
			e.printStackTrace();
			return e.getMessage().toString();
		}
		return name;
	}
	
	@GetMapping("/query")
	public Map<String,JSONArray> query(String version) {
		Map<String, JSONArray> result = new HashMap<String, JSONArray>();
		try {
			String stepName = STEP_NAME+version;
			String relationName = RELATION_NAME+version;
			DgraphSteps steps = service.getDgraphSteps(stepName);
			JSONArray steps_arr = JSONArray.fromObject(steps.getSteps());
			DgraphRelations relations = service.getDgraphRelation(relationName);
			List<Relation> rels = service.checkOccupy(relations.getRelations());
			JSONArray rel_arr = JSONArray.fromObject(rels);
			result.put("nodeDataArray", steps_arr);
			result.put("linkDataArray", rel_arr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * <p>Title: savePos</p>  
	 * <p>Description: </p>  
	 * @param map
	 * @param version
	 * @return
	 */
	@PutMapping("/save")
	public String savePos(@RequestBody Map<String,Object> map) {
		String result = "success";
		Object steps = map.get("nodeDataArray");
		Object relations = map.get("linkDataArray");
		String version = (String) map.get("version");
		//service.saveStepsPorts(steps,version);
		service.saveStepsPortsOld(steps, version);
		service.saveLinkPoints(relations, version);
		return result;
	}
	
	/**
	 * 
	 * <p>Title: query</p>  
	 * <p>Description: </p>  
	 * @param version
	 * @return
	 */
	@GetMapping("/queryNew")
	public Map<String,Object> queryNew(String version) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			String stepName = STEP_NAME+version;
			String relationName = RELATION_NAME+version;
			DgraphSteps steps = service.getDgraphSteps(stepName);
			JSONArray steps_arr = JSONArray.fromObject(steps.getSteps());
			DgraphRelations relations = service.getDgraphRelation(relationName);
			List<Relation> rels = service.checkOccupy(relations.getRelations());
			JSONArray rel_arr = JSONArray.fromObject(rels);
			JSONArray linkDataArray = null;
			if(relations.getLinks() == null) {
				linkDataArray = JSONArray.fromObject("[]");
			}else {
				linkDataArray = JSONArray.fromObject(relations.getLinks());
			}
			Map<String,Map<String,List<String>>> map = service.fzMap();
			result.put("nodeDataArray", steps_arr);
			result.put("linkRel", rel_arr);
			result.put("linkDataArray", linkDataArray);
			result.put("fzRel", map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 
	 * <p>Title: saveNewPos</p>  
	 * <p>Description: </p>  
	 * @param map
	 * @return
	 */
	@PutMapping("/saveNew")
	public String saveNewPos(@RequestBody Map<String,Object> map) {
		String result = "success";
		Object links = map.get("linkDataArray");
		Object steps = map.get("nodeDataArray");
		String version = (String) map.get("version");
		service.saveStepsPorts(steps,version);
		service.saveLink(links, version);
		return result;
	}
	
	/**
	 * 
	 * <p>Title: getAllVersion</p>  
	 * <p>Description: </p>  
	 * @return
	 */
	@GetMapping("/getAllVersion")
	public List<String> getAllVersion(String version){
		List<String> allVersion = service.getAllVersion(version);
		return allVersion;
	}
	
	/**
	 * 
	 * <p>Title: delete</p>  
	 * <p>Description: </p>  
	 * @param version
	 * @return
	 */
	@GetMapping("/delete")
	public String delete(String version) {
		String result = "success";
		service.deleteDgraph(version);
		return result;
	}
	
	/**
	 * 暂时没用
	 * <p>Title: getFzRel</p>  
	 * <p>Description: </p>  
	 * @param version
	 * @return
	 */
	@GetMapping("/getFz")
	public Map<String,Map<String,List<String>>> getFzRel(String version){
		Map<String,Map<String,List<String>>> map = new HashMap<>();
		map = service.getFzRel(version);
		return map;
	}
}	
